package visual;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import bd.JDBCpartida;

import logic.RegistroDatos;
import logic.partida;

public class CargarPartida extends JFrame implements ActionListener, MouseListener{
		
	private static final long serialVersionUID = 1L;
	private static partida p;
	public static partida getPartida(){
		return p;
	}

	private JPanel sup;
	private JPanel inf;
	private JTable tabla;
	private DefaultTableModel X;
	private JScrollPane j;
	private JButton Cargar;
	private JButton Cancelar;
	private int click;
	private String fecha;
	private static ArrayList<partida> partidaList = new ArrayList<partida>();
		
	public CargarPartida (){
		this.p=null;
		JDBCpartida jp = new JDBCpartida();
		try{
			partidaList = jp.selectAll();
		}catch(SQLException a){
		}catch(IllegalArgumentException b){
		}
			
		//ASPECTO GENERAL DE LA VENTANA
		this.setSize(500,400);
		this.setResizable(false);
		this.setTitle("CARGAR PARTIDA");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		sup = new JPanel();
		inf = new JPanel();
			
		Cargar = new JButton("Cargar");
		Cancelar = new JButton("Cancelar");
					
		X = new DefaultTableModel();
		X.addColumn("J1");
		X.addColumn("J2");
		X.addColumn("Fecha");
		int i = 0;
		while(i<partidaList.size()){
			partida p = partidaList.get(i);
			String[] atributos = {p.getJ1(),p.getJ2(),p.getDate()};
			if((RegistroDatos.getNick1().equals(p.getJ1()))&&(RegistroDatos.getNick2().equals(p.getJ2()))){
				X.addRow(atributos);
			}			
			i = i + 1;
		}
		tabla = new JTable(X);
		j = new JScrollPane(tabla);
			
		j.setPreferredSize(new Dimension(270,270));
		sup.add(j);
		inf.add(Cargar);
		inf.add(Cancelar);
		
		this.setLayout(new BorderLayout());
		this.add("North",new JPanel());
		this.add("Center", sup);
		this.add("South", inf);	
		
		//ACTION LSITENERS
		Cargar.addActionListener(this);
		Cancelar.addActionListener(this);
		tabla.addMouseListener(this);
	}
		
	//REALIZA LA OPERACION CORRESPONDIENTE A CADA BOTON
	public void actionPerformed(ActionEvent e){
		if(e.getSource()==Cancelar){
			this.dispose();
			MenuPrincipal mP = new MenuPrincipal(true);
			mP.setVisible(true);
		}else if(e.getSource()==Cargar){
			int row= tabla.getSelectedRow();
			JDBCpartida p=new JDBCpartida();
			this.p=partidaList.get(row);
			try {
				this.p=p.selectOne(p);
			} catch (Exception e1) {e1.printStackTrace();}
			this.dispose();
			VentanaTablero vT = new VentanaTablero(fecha,true);
			vT.setVisible(true);
		}	
	}	
		
	public void mouseClicked(MouseEvent e){
		click = tabla.rowAtPoint(e.getPoint());
		fecha = ((String)X.getValueAt(click, 2));				
	}

	public void mouseEntered(MouseEvent e) {}

	public void mouseExited(MouseEvent e){}

	public void mousePressed(MouseEvent e){}

	public void mouseReleased(MouseEvent e){}
	
	public static void main (String[]args){
		CargarPartida rc = new CargarPartida();
		rc.setVisible(true);
	}
}

